from fastapi import APIRouter, Depends, status
from sqlalchemy.ext.asyncio import AsyncSession

from database.database import get_db
from core.base.api_mode import ApiResponse, api_success
from core.pagination import DependsPagination, PageData

from modules.{{ app_name }}.services.{{ filename }}_service import {{ class_name }}Service
from modules.{{ app_name }}.schemas.{{ filename }}_schema import {{ class_name }}Create, {{ class_name }}Update
from modules.{{ app_name }}.params.{{ filename }}_params import {{ class_name }}Query
from modules.{{ app_name }}.vos.{{ filename }}_vo import {{ class_name }}Response


router = APIRouter()


@router.post(
    "/list",
    response_model=ApiResponse[PageData[{{ class_name }}Response]],
    summary="获取{{ table_comment }}列表",
    dependencies=[DependsPagination],
)
async def list_{{ table_name }}s(query_params: {{ class_name }}Query, db: AsyncSession = Depends(get_db)):
    result = await {{ class_name }}Service.get_list_with_pagination(db, query_params)
    return api_success(data=result, msg="获取成功")


@router.get("/detail/{pk}", response_model=ApiResponse[{{ class_name }}Response], summary="获取{{ table_comment }}详情")
async def get_{{ table_name }}(pk: int, db: AsyncSession = Depends(get_db)):
    result = await {{ class_name }}Service.get_by_id(db, pk)
    return api_success(data=result, msg="获取成功")


@router.post("/create", response_model=ApiResponse[{{ class_name }}Response], status_code=status.HTTP_201_CREATED, summary="创建{{ table_comment }}")
async def create_{{ table_name }}(body: {{ class_name }}Create, db: AsyncSession = Depends(get_db)):
    result = await {{ class_name }}Service.create(db, body)
    return api_success(data=result, msg="创建成功")


@router.put("/update/{pk}", response_model=ApiResponse[{{ class_name }}Response], summary="更新{{ table_comment }}")
async def update_{{ table_name }}(pk: int, body: {{ class_name }}Update, db: AsyncSession = Depends(get_db)):
    result = await {{ class_name }}Service.update(db, pk, body)
    return api_success(data=result, msg="更新成功")


@router.delete("/delete/{pk}", response_model=ApiResponse[dict], summary="删除{{ table_comment }}")
async def delete_{{ table_name }}(pk: int, db: AsyncSession = Depends(get_db)):
    ok = await {{ class_name }}Service.delete(db, pk)
    return api_success({"success": ok}, msg="删除成功" if ok else "未删除")
